//
//  LoginVC.m
//  EJL
//
//  Created by adc on 15/11/4.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "ZhuCeDL.h"
#import "ChangeThePasswordVC.h"
#import "HomeVC.h"
//#import "WangJiMiMaViewController.h"
@interface LoginVC ()

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
   // self.navigationController.navigationBarHidden = NO;
   
   // self.title=@"登录";
    UIView*viewbg=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE , 44)];
    viewbg.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:viewbg];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35,7, 70,30)];
    label.text=@"用户登录";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [viewbg addSubview:label];
    UIImageView*bgImage=[[UIImageView alloc]initWithFrame:CGRectMake((WIDE-150)/2,70,150, 140)];
    bgImage.image=[UIImage imageNamed:@"login"];
    [self.view addSubview:bgImage];

    UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30, 30)];
    [but1 setImage:[UIImage imageNamed:@"返回箭头1"] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [viewbg addSubview:but1];
    UIView *View1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage1=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage1 setImage:[UIImage imageNamed:@"用户图标"]];
    [View1 addSubview:viewImage1];
    _shouJiFiekd.leftView=View1;
    
    _shouJiFiekd.leftViewMode=UITextFieldViewModeAlways;
    [_shouJiFiekd setLeftViewMode:UITextFieldViewModeAlways];
    
    UIView *View2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage2=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage2 setImage:[UIImage imageNamed:@"密码图标"]];
    [View2 addSubview:viewImage2];
    _miMaField.leftView=View2;
    
    _miMaField.leftViewMode=UITextFieldViewModeAlways;
    [_miMaField setLeftViewMode:UITextFieldViewModeAlways];
    
}
-(void)backClick
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)butClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

- (IBAction)dengLu:(UIButton *)sender
{
     
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];

    
    ZhuCeDL*dengLu=[[ZhuCeDL alloc]init];
   
    dengLu.password= [MD5 md532BitLower:_miMaField.text ] ;
    dengLu.phone= _shouJiFiekd.text;
    if ([dengLu.phone length]&&[dengLu.password length]) {
        [HttpsManager LoginRequestWithPhoneNumber:dengLu completion:^(int errcode, NSString *errmsg)
         {
              HUD.textLabel.text = @"正在登录中";
             NSString*str=[NSString stringWithFormat:@"%d",errcode];
             if ([str isEqualToString:@"0"])
             {
                 
//                 HomeVC*vc=[[HomeVC alloc]init];
//                 [self.navigationController pushViewController:vc animated:YES];
                 [self.navigationController popToRootViewControllerAnimated:YES];
                
                 [HUD dismiss];
                 return ;
             }
             HUD.textLabel.text = errmsg;
              [HUD dismissAfterDelay:0.5];
         }];

    }else{
       // SHOW_ALERT(@"请输入正确的账号和密码");
         HUD.textLabel.text = @"请输入正确的账号和密码";
        [HUD dismissAfterDelay:0.5];

    }
    
}
-(void)goToMain
{
    HomeVC *homeVC = [[HomeVC alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    self.view.window.rootViewController = nav;
}
- (IBAction)zhuCe:(UIButton *)sender
{
    RegisterVC*vc=[[RegisterVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

- (IBAction)wangJi:(UIButton *)sender
{
    
//    WangJiMiMaViewController*wJ=[[WangJiMiMaViewController alloc]init];
//    [self.navigationController pushViewController:wJ animated:YES];
    ChangeThePasswordVC*vc=[[ChangeThePasswordVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
    
}
@end
